package cn.shopifymall.camunda717.listener;

import cn.shopifymall.camunda717.api.ProcessApi;
import cn.shopifymall.camunda717.vo.OperationVo;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.impl.core.model.CoreModelElement;
import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity;
import org.camunda.bpm.spring.boot.starter.event.ExecutionEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class GlobalExecutionListener {

    @EventListener
    public void onExecutionEvent(DelegateExecution delegateExecution) {
        ExecutionEntity executionEntity = (ExecutionEntity) delegateExecution;
        CoreModelElement eventSource = executionEntity.getEventSource();
        String processDefinitionId = delegateExecution.getProcessDefinitionId();
        if ("end".equals(delegateExecution.getEventName()) && processDefinitionId.equals(eventSource.getId())) {
            //由流程定义触发的结束，流程最终的结束
            OperationVo operationVo = ProcessApi.operationVoHolder.get();
            ProcessApi.operationVoHolder.remove();
            throw new RuntimeException("出错了");
        }

    }

    @EventListener
    public void onExecutionEvent(ExecutionEvent executionEvent) {
        // 处理不可变的任务事件
    }
}
